package com.egg.ylt.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.heartbeat.HeartbeatManageSingleton;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.eventbus.EventCenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_UserSetting extends BaseActivity {


    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_rl_view)
    RelativeLayout includeRlView;
    @BindView(R.id.tv_set_phone)
    TextView tvSetPhone;
    @BindView(R.id.ll_set_phone)
    LinearLayout llSetPhone;
    @BindView(R.id.tv_set_password)
    TextView tvSetPassword;
    @BindView(R.id.ll_set_password)
    LinearLayout llSetPassword;
    @BindView(R.id.tv_set_code)
    TextView tvSetCode;

    private AppSharedPreferences mSp;
    private String mPhone;


    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_user_setting;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.color_f3f3f3);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, includeRlView);
        //设置title
        includeTvTitle.setText("设置");

        mSp = new AppSharedPreferences(mContext);
        mPhone = mSp.getString(Constants.USER_PHONE);
        //手机号
        tvSetPhone.setText(CustomUtils.setPhoneHide(mPhone));

        //版本号
        String appVersionCode = StringUtil.getAppVersionName(mContext);
        tvSetCode.setText(appVersionCode);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPhone = mSp.getString(Constants.USER_PHONE);
        //手机号
        tvSetPhone.setText(CustomUtils.setPhoneHide(mPhone));
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.ll_set_phone, R.id.ll_set_password, R.id.btn_set_exit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back:
                finish();
                break;
            case R.id.ll_set_phone:
                readyGo(ACT_UpdatePhone.class);
                break;
            case R.id.ll_set_password:
                Bundle mBundle = new Bundle();
                mBundle.putString("phone", mPhone);
                mBundle.putInt("type", Constants.UPDATE_PHONE_TO_CODE);
                readyGo(ACT_ImportCode.class, mBundle);
                break;
            case R.id.btn_set_exit://退出登录
                showNormalDialog();
                break;
        }
    }

    /**
     * 退出登录
     * 系统自带的dialog
     */
    private void showNormalDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(mContext);
        normalDialog.setTitle("退出登录");
//        normalDialog.setMessage("v1.0.3版本");
        normalDialog.setCancelable(false);// 设置是否可以通过点击Back键取消，或者点击区域以外的地方
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //确定
                        //清空本地数据
                        mSp.emptyData();
                        //初始化公共参数
                        Constants.TOKEN = "";
                        Constants.SHOPID = "";
                        Constants.USERID = "";
                        Constants.BABYID = "";
                        Constants.RELATIONSHIP = "";
                        Constants.SWIMRINGCODE = "";
                        //释放堆栈内的所有资源
//                        ActivityTaskManager.getInstance().closeAllActivity();
                        EventBus.getDefault().post(new EventCenter(Constants.EXIT_LOGIN_GO_HOME));
                        // 停止心跳轮询
                        HeartbeatManageSingleton.getInstance.stopAllHeartbeat();
                        dialog.cancel();
                        finish();
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //取消
                        dialog.cancel();
                    }
                });

        // 显示
        normalDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
